package com.joeyzh.base;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.joey.ui.adapter.BaseRecyclerJsonAdapter;
import com.joey.ui.adapter.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Joey on 2018/12/3.
 * 用于RecyclerView的控制部分
 *
 * @author by Joey
 */

public class RecyclerViewModel<T> extends ListViewModel<T> {

    private RecyclerView recyclerView;
    private BaseRecyclerJsonAdapter<T, ? extends RecyclerView.ViewHolder> mAdapter;
    private OnItemClickListener<T> onItemClickListener;
    protected Context mContext;

    public RecyclerViewModel(List<T> list) {
        super(list);
    }

    @Override
    public ListViewModel builder(View listRoot, Context context, int layoutId) {
        super.builder(listRoot, context, layoutId);
        mContext = context;
        this.recyclerView = (RecyclerView) listRoot;
        mAdapter = new BaseRecyclerJsonAdapter(context, entityBean, layoutId, from, to) {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View convertView = null;
                convertView = LayoutInflater.from(
                        context).inflate(layoutId, parent,
                        false);
                return new RecyclerView.ViewHolder(convertView) {

                };
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                onBindViewItem(entityBean.get(position), holder, position);
            }
        };
        recyclerView.setAdapter(mAdapter);
        if (onItemClickListener != null) {
            mAdapter.setOnItemClickListener(onItemClickListener);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        return this;
    }

    @Override
    public List<T> getEntityBean() {
        return super.getEntityBean();
    }

    public void onBindViewItem(T item, RecyclerView.ViewHolder holder, int position) {

    }

    public void setOnItemClickListener(OnItemClickListener<T> listener) {
        if (mAdapter == null) {
            return;
        }
        onItemClickListener = listener;
        mAdapter.setOnItemClickListener(listener);
    }

    public <Adapter extends BaseRecyclerJsonAdapter<T, ? extends RecyclerView.ViewHolder>> Adapter getAdapter() {
        return (Adapter) mAdapter;
    }

    /**
     * 添加数据 进行刷新
     *
     * @param bean
     */
    public void addEntityBean(T bean) {
        entityBean.add(bean);
        mAdapter.notifyDataSetChanged();
    }

    public void addEntityBean(int index, T bean) {
        entityBean.add(index, bean);
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 清空数据
     */
    public void clearEntity() {
        entityBean.clear();
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 修改数据
     *
     * @param bean
     * @param position
     */
    public void updateEntity(T bean, int position) {
        entityBean.set(position, bean);
        mAdapter.notifyDataSetChanged();
    }


    /**
     * 修改数据
     *
     * @param beanLis
     */
    public void updateEntity(List<T> beanLis) {
        entityBean.clear();
        List<T> list = new ArrayList<>();
        list.addAll(beanLis);
        entityBean.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param beanLis
     */
    public void addAllEntity(List<T> beanLis) {
        ArrayList<T> list = new ArrayList<>();
        list.addAll(beanLis);
        entityBean.addAll(list);
        if (null != mAdapter) {
            mAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 刷新数据
     */
    public void notifyDataSetChanged() {
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 删除数据
     *
     * @param position
     */
    public void deleteEntity(int position) {
        entityBean.remove(position);
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 设置列表显示方式
     *
     * @param context
     * @param count
     */
    public void setLayoutManager(Context context, int count) {
        recyclerView.setLayoutManager(new GridLayoutManager(context, count));
    }

}
